package com.kewill.jira.servlet;

import com.kewill.jira.common.DaoAnnotation;
import com.kewill.jira.common.JiraAttribute;
import com.kewill.jira.common.ReleaseNotesHandler;
import com.kewill.jira.dao.IIssueDao;
import com.kewill.jira.model.ReleaseNoteIssue;
import com.kewill.jira.util.ServletUtil;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;

/**
 * Created by YanJun on 2016/7/4.
 */
public class IssueReleaseNoteServlet extends IssueBaseServlet {

    private Logger issueReleaeNoteLog = LoggerFactory.getLogger(IssueReleaseNoteServlet.class);
    private IIssueDao issueDao;

    @DaoAnnotation("IssueDaoImpl")
    public void setIssueDao(IIssueDao issueDao) {
        this.issueDao = issueDao;
    }

    public void prepareFixVersions(HashMap<String,Object> servletMetaMap)
            throws ServletException, IOException {
        HttpServletResponse response = (HttpServletResponse)servletMetaMap.get(JiraAttribute.PARAM_RESPONSE);
        issueReleaeNoteLog.debug("invoke IssueReleaseNoteServlet.prepareFixVersions()");
        //IIssueDao issueDao = new IssueDaoImpl();
        List<String> versionList = issueDao.getAllFixVersion();
        StringBuilder versionBuilder = new StringBuilder();
        for(String version:versionList){
            versionBuilder.append("<option>"+version+"</option>");
        }
        PrintWriter out = response.getWriter();
        out.write(versionBuilder.toString());
        out.flush();
    }

    public void generateReleaseNoteReport(HashMap<String,Object> servletMetaMap)
            throws ServletException, IOException{
        issueReleaeNoteLog.debug("invoke IssueReleaseNoteServlet.generateReleaseNoteReport()");

        HttpServletRequest request = (HttpServletRequest) servletMetaMap.get(JiraAttribute.PARAM_REQUEST);
        HttpServletResponse response = (HttpServletResponse) servletMetaMap.get(JiraAttribute.PARAM_RESPONSE);
        response.setContentType("application/x-msdowmload");
        response.addHeader("Content-Disposition", "attachment;filename=" + JiraAttribute.RELEASENOTE_FILENAME);
        VelocityEngine ve = new VelocityEngine();
        Properties properties = new Properties();
        VelocityContext context = new VelocityContext();
        String fileDir = IssueReleaseNoteServlet.class.getResource("/template").getPath();
        properties.setProperty(ve.FILE_RESOURCE_LOADER_PATH, fileDir);
        try {
            String version = ServletUtil.getParam(request,"projectVersion");
            ve.setProperty(JiraAttribute.VELOCITY_RESOURCE_LOADER, JiraAttribute.VELOCITY_RESOURCE_LOADER_value);
            ve.setProperty(JiraAttribute.VELOCITY_CLASS_RESOURCE_LOADER, JiraAttribute.VELOCITY_CLASS_RESOURCE_LOADER_VALUE);
            ve.init();
            Template template = ve.getTemplate(JiraAttribute.VELOCITY_RELEASENOTE_TEMPLATE, "utf-8");
            PrintWriter out = response.getWriter();
            List<ReleaseNoteIssue> releaseNoteIssueList = ReleaseNotesHandler.setReleaseNotesFromProject(context, version);
            template.merge(context, out);
            if(ServletUtil.getParam(request,"prepareTar") != null){
                ReleaseNotesHandler.prepareTar(version,releaseNoteIssueList);
            }
            out.flush();
            out.close();
        } catch (ResourceNotFoundException e) {
            e.printStackTrace();
        } catch (ParseErrorException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
